//
//  ECMemberAddTableViewCell.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/11/3.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECMemberAddTableViewCell.h"

@interface ECMemberAddTableViewCell()

@property (nonatomic,strong) CAShapeLayer * bottomLine;

@property (nonatomic,strong) UILabel * titleLB;

@property (nonatomic,strong) UISwitch * switchBT;

@property (nonatomic,strong) UIImageView * accessoryV;

@end

@implementation ECMemberAddTableViewCell
@synthesize isON = _isON;
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    self.titleLB.text = nil;
    self.detailLB.text = nil;
    self.detailLB.attributedText = nil;
    self.detailLB.textColor = ECCOLOR(0xcccccc);
    self.detailTF.text = nil;
    self.detailTF.placeholder = nil;
    self.switchBT.on = NO;
    self.isNeed = NO;
    self.accessoryImg = [UIImage imageNamed:@"more_member"];
    self.accessoryV.hidden = YES;
    [self setCellStyle:ECMemberAddCellStyleTxt];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (!self.bottomLine) {
        self.bottomLine = [CAShapeLayer layer];
        self.bottomLine.borderWidth = 0.25 * PX_RELATIVE375;
        self.bottomLine.borderColor = ECCOLOR(0xf2f2f2).CGColor;
        [self.contentView.layer addSublayer:self.bottomLine];
    }
    self.bottomLine.frame = CGRectMake(12 * PX_RELATIVE375, self.contentView.height-0.5*PX_RELATIVE375, self.contentView.width - 24 * PX_RELATIVE375, 0.5 * PX_RELATIVE375);
}

- (void)setupUI {
    __weak typeof(self) weakSelf = self;
    self.titleLB = [UILabel new];
    self.titleLB.font = ECFONT(14);
    self.titleLB.textColor = ECCOLOR(0x333333);
    [self.contentView addSubview:self.titleLB];
    [self.titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.contentView);
        make.left.mas_equalTo(weakSelf.contentView).mas_offset(18 * PX_RELATIVE375);
    }];
    
    self.accessoryV = [UIImageView new];
    self.accessoryV.image = [UIImage imageNamed:@"more_member"];
    self.accessoryV.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:self.accessoryV];
    [self.accessoryV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.contentView).mas_offset(-1*PX_RELATIVE375);
        make.right.mas_equalTo(weakSelf.contentView).mas_offset(-10 * PX_RELATIVE375);
//        make.width.height.mas_equalTo(20* PX_RELATIVE375);
    }];
    self.accessoryV.hidden = YES;
    
    self.detailTF = [UITextField new];
    self.detailTF.textColor = ECCOLOR(0x333333);
    self.detailTF.font = ECFONT(13 * PX_RELATIVE375);
    self.detailTF.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:self.detailTF];
    [self.detailTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.contentView).mas_offset(-18 * PX_RELATIVE375);
        make.top.bottom.mas_equalTo(weakSelf.contentView);
        make.left.mas_greaterThanOrEqualTo(weakSelf.titleLB.mas_right).mas_offset(12*PX_RELATIVE375);
    }];
    
    self.detailLB = [UILabel new];
    self.detailLB.font = ECFONT(13 * PX_RELATIVE375);
    self.detailLB.textColor = ECCOLOR(0xcccccc);
    [self.contentView addSubview:self.detailLB];
    [self.detailLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.contentView);
        make.right.mas_equalTo(weakSelf.contentView).mas_offset(-35 * PX_RELATIVE375);
        make.left.mas_greaterThanOrEqualTo(weakSelf.titleLB.mas_right).mas_offset(12*PX_RELATIVE375);
    }];
    self.detailLB.hidden = YES;
    
    self.switchBT = [UISwitch new];
    [self.switchBT addTarget:self action:@selector(changeSwitchState:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.switchBT];
    [self.switchBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.contentView);
        make.right.mas_equalTo(weakSelf.contentView).mas_offset(-16 * PX_RELATIVE375);
    }];
    self.switchBT.hidden = YES;
}

- (void)setCellStyle:(ECMemberAddCellStyle)cellStyle {
    _cellStyle = cellStyle;
    __weak typeof(self) weakSelf = self;
    switch (_cellStyle) {
        case ECMemberAddCellStyleTxt:
        {
            self.detailTF.hidden = NO;
            self.detailLB.hidden = YES;
            self.accessoryV.hidden = YES;
            self.switchBT.hidden = YES;
            [self.detailLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(weakSelf.contentView).mas_offset(-35 * PX_RELATIVE375);
            }];
        }
            break;
        case ECMemberAddCellStyleMore:{
            self.detailTF.hidden = YES;
            self.detailLB.hidden = NO;
            self.accessoryV.hidden = NO;
            self.switchBT.hidden = YES;
            [self.detailLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(weakSelf.contentView).mas_offset(-35 * PX_RELATIVE375);
            }];
        }
            break;
        case ECMemberAddCellStyleSwitch:{
            self.detailTF.hidden = YES;
            self.detailLB.hidden = NO;
            self.accessoryV.hidden = YES;
            self.switchBT.hidden = NO;
            [self.detailLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(weakSelf.contentView).mas_offset(-72 * PX_RELATIVE375);
            }];
        }
            break;
        default:
            break;
    }
}

- (void)changeSwitchState:(UISwitch*)sender {
    [self setIsON:sender.isOn];
}

- (void)setIsNeed:(BOOL)isNeed {
    _isNeed = isNeed;
    if (self.title) {
        [self setTitle:self.title];
    }
}

- (void)setTitle:(NSString *)title {
    _title = title;
    if (self.isNeed && _title && _title.length > 0) {
        NSMutableAttributedString * muAStr = [[NSMutableAttributedString alloc] initWithString:_title];
        [muAStr addAttributes:@{NSForegroundColorAttributeName:ECCOLOR(0xF94233)} range:NSMakeRange(_title.length -1, 1)];
        self.titleLB.attributedText = muAStr;
    }else {
        self.titleLB.text = _title;
    }
}

- (void)setIsON:(BOOL)isON {
    _isON = isON;
    [self.switchBT setOn:_isON animated:YES];
    if (self.cellStyle != ECMemberAddCellStyleSwitch) {
        return;
    }
    if (_isON) {
        self.detailLB.text = @"正常";
    }else {
        self.detailLB.text = @"禁止";
    }
}

- (void)setAccessoryImg:(UIImage *)accessoryImg {
    _accessoryImg = accessoryImg;
    self.accessoryV.image = _accessoryImg;
}

- (BOOL)isON {
    return self.switchBT.isOn;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
